package com.sihai.ehr.web.action.contract;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.ContractPerson;
import com.sihai.ehr.service.ContractService;
import com.sihai.ehr.web.action.EHRAction;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

/**
 * @author: wxn
 * @since: 12-6-22 下午18:37
 *
 * 合同人员设置管理Action
 */

@Results({@Result(name = "personnel_list", location = "personnel_list.jsp"),
        @Result(name = "personnel_main", location = "contract_main.jsp"),
        @Result(name = "personnel_add", location = "personnel_add.jsp")})
public class PersonnelAction extends EHRAction{

    @BeanInject
    private ContractService contractService;

    private LimitedList<ContractPerson> persons;

    private ContractPerson person;

    private String ids; /*删除的版本ID列表(逗号分隔)*/

    @Action(value = "/contract/personnel")
    public String personnel() throws Exception {
        listAction = "/contract/personnel_list.xhtml";
        listTitle = "合同人员设置";
        
        return "personnel_main";
    }

    //合同版本列表
    @Action(value = "/contract/personnel_list")
    @Override
    public String execute() throws Exception {
        persons = contractService.findPersonsByOrganizationId(organizationId, createLimit(), createSorter());

        //setCount(3);
        //person = contractService.findpersonnels(v, createLimit(), createSorter());

        /* 分页 */
        this.paging(persons);
        /* 设置翻页action */
        this.setPageUrl("contract/personnel_list.xhtml");

        return "personnel_list";
    }

    //跳转到合同版本添加页面
    @Action(value = "/contract/personnel_edit")
    public String edit() throws Exception {
        if(person != null && person.getId() != null){
                person = contractService.getPerson(person.getId());
        }

        return "personnel_add";
    }

    //添加/修改合同人员设置
    @Action(value = "/contract/personnel_save")
    public String save() throws Exception {
        if(person != null){
            if(person.getId() != null){//edit
                ContractPerson p = contractService.getPerson(person.getId());
                p.setDuty(person.getDuty());
                p.setContractVersion(person.getContractVersion());
                contractService.savePerson(p);
            }else{//create
                contractService.savePerson(person);
            }
        }
        
        return personnel();
    }


    @Action(value = "/contract/personnel_remove")
    public String remove() throws Exception {
        if(StringUtils.isNotBlank(ids)){
            contractService.removePersons(ids);
        }

        return execute();
    }

    public String getIds() {
        return ids;
    }

    public void setIds(String ids) {
        this.ids = ids;
    }

    public LimitedList<ContractPerson> getPersons() {
        return persons;
    }

    public void setPersons(LimitedList<ContractPerson> persons) {
        this.persons = persons;
    }

    public ContractPerson getPerson() {
        return person;
    }

    public void setPerson(ContractPerson person) {
        this.person = person;
    }
}
